package me.zhengjie.modules.web.service.config.impl;

import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.service.RichTextService;
import me.zhengjie.modules.system.service.TermsRulesService;
import me.zhengjie.modules.system.service.dto.RichTextDto;
import me.zhengjie.modules.system.service.dto.RichTextQueryCriteria;
import me.zhengjie.modules.system.service.dto.TermsRulesDto;
import me.zhengjie.modules.system.service.dto.TermsRulesQueryCriteria;
import me.zhengjie.modules.web.domain.config.RichTextResp;
import me.zhengjie.modules.web.domain.config.TermsRulesResp;
import me.zhengjie.modules.web.service.config.IConfigService;
import me.zhengjie.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王宇
 */
@Service
public class ConfigServiceImpl implements IConfigService {

    @Autowired
    private RichTextService richTextService;

    @Autowired
    private TermsRulesService termsRulesService;

    @Override
    public RichTextResp getRichTextByType(String type) {

        if (StringUtils.isBlank(type)){
            throw new WeChatException("类型不能为空");
        }
        if("richtext_type_privacy".equals(type)){
            TermsRulesResp termsRulesByCode = getTermsRulesByCode(type);
            if (termsRulesByCode == null){
                RichTextResp resp = new RichTextResp();
                resp.setContent(termsRulesByCode.getContent());
                resp.setTitle(termsRulesByCode.getTitle());
                resp.setAllContent(termsRulesByCode.getContent());
                resp.setId(termsRulesByCode.getId().intValue());
                resp.setTextKey( type);
                return resp;
            }
        }
        RichTextQueryCriteria criteria = new RichTextQueryCriteria();
        criteria.setTextKey(type);
        List<RichTextDto> richTextDtos = richTextService.queryAll(criteria);
        if (CollectionUtils.isEmpty(richTextDtos)){
            return null;
        }
        RichTextResp resp = new RichTextResp();
        RichTextDto richTextDto = richTextDtos.get(0);
        BeanUtils.copyProperties(richTextDto,resp);
        return resp;
    }

    @Override
    public List<RichTextResp> getRichTextListByType(String type) {
        RichTextQueryCriteria criteria = new RichTextQueryCriteria();
        criteria.setTextKey(type);
        List<RichTextDto> richTextDtos = richTextService.queryAll(criteria);
        if (CollectionUtils.isEmpty(richTextDtos)){
            return Collections.emptyList();
        }
        List<RichTextResp> resp = new ArrayList<>();
        richTextDtos.forEach(richTextDto1 -> {
            RichTextResp richTextResp = new RichTextResp();
            BeanUtils.copyProperties(richTextDto1,richTextResp);
            resp.add(richTextResp);
            });
        return resp;
    }

    @Override
    public List<TermsRulesResp> getTermsRulesList() {
        TermsRulesQueryCriteria criteria = new TermsRulesQueryCriteria();
        criteria.setStatus(ConstantUtils.Valid);
        List<TermsRulesDto> list = termsRulesService.queryAll(criteria);
        if (CollectionUtils.isEmpty(list)){
            return Collections.emptyList();
        }
        list = list.stream().sorted(Comparator.comparing(TermsRulesDto::getSort)).collect(Collectors.toList());
        List<TermsRulesResp> resp = new ArrayList<>();
        for (TermsRulesDto termsRulesDto : list) {
            TermsRulesResp termsRulesResp = new TermsRulesResp();
            termsRulesResp.setId(termsRulesDto.getId());
            termsRulesResp.setTitle(termsRulesDto.getTitle());
            termsRulesResp.setContent(termsRulesDto.getContent());
            termsRulesResp.setSort(termsRulesDto.getSort());
            termsRulesResp.setUpdateTime(DateUtil.getTimeStamp(termsRulesDto.getGmtModified()));
            termsRulesResp.setKey(termsRulesDto.getRuleCode());
            resp.add(termsRulesResp);
        }
        return resp;
    }

    @Override
    public List<TermsRulesResp> getTermsRulesListV2(Integer type) {
        TermsRulesQueryCriteria criteria = new TermsRulesQueryCriteria();
        criteria.setStatus(ConstantUtils.Valid);
        criteria.setType(type);
        List<TermsRulesDto> list = termsRulesService.queryAll(criteria);
        if (CollectionUtils.isEmpty(list)){
            return Collections.emptyList();
        }
        list = list.stream().sorted(Comparator.comparing(TermsRulesDto::getSort)).collect(Collectors.toList());
        List<TermsRulesResp> resp = new ArrayList<>();
        for (TermsRulesDto termsRulesDto : list) {
            TermsRulesResp termsRulesResp = new TermsRulesResp();
            termsRulesResp.setId(termsRulesDto.getId());
            termsRulesResp.setTitle(termsRulesDto.getTitle());
            //termsRulesResp.setContent(termsRulesDto.getContent());
            termsRulesResp.setSort(termsRulesDto.getSort());
            termsRulesResp.setUpdateTime(DateUtil.getTimeStamp(termsRulesDto.getGmtModified()));
            termsRulesResp.setKey(termsRulesDto.getRuleCode());
            resp.add(termsRulesResp);
        }
        return resp;
    }

    @Override
    public TermsRulesResp getTermsRulesByCode(String key) {
        TermsRulesQueryCriteria criteria = new TermsRulesQueryCriteria();
        criteria.setStatus(ConstantUtils.Valid);
        criteria.setRuleCode(key);
        List<TermsRulesDto> list = termsRulesService.queryAll(criteria);
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        TermsRulesDto termsRulesDto = list.get(0);
        TermsRulesResp termsRulesResp = new TermsRulesResp();
        termsRulesResp.setId(termsRulesDto.getId());
        termsRulesResp.setTitle(termsRulesDto.getTitle());
        termsRulesResp.setContent(termsRulesDto.getContent());
        termsRulesResp.setSort(termsRulesDto.getSort());
        termsRulesResp.setUpdateTime(DateUtil.getTimeStamp(termsRulesDto.getGmtModified()));
        termsRulesResp.setKey(termsRulesDto.getRuleCode());
        return termsRulesResp;
    }
}
